package darkwindclient.connection;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import darkwindclient.gui.TextField;

/**
 * @author Jacob Ferm 
 */
public class Connection {
	Reader r;
	Writer w;
	Socket sock;

	/**
	 * @param text
	 * @param log
	 */
	public Connection(TextField text, TextField log) {
		try {
			sock = new Socket("darkwind.org", 3000);
			r = new Reader(sock.getInputStream(), text, log);
			w = new Writer(sock.getOutputStream());

			r.start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void close() {
		// close socket and terminate threads
		try {
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void write(String text) {
		w.write(text);
	}

}
